package be.donovan.gui.helper;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.synth.SynthLookAndFeel;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.java.swing.plaf.motif.MotifLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

import be.donovan.gui.ScreenController;
import be.donovan.gui.helper.components.JErrorDialog;
import be.donovan.gui.helper.startup.ProgressBarProgressMonitor;
import be.donovan.gui.helper.startup.ProgressMonitoringBeanFactoryPostProcessor;
import be.donovan.gui.helper.startup.ProgressSplashScreen;

public class Starter {
	private static final Starter instance = new Starter();

	private Starter() {
//		LookAndFeel laf = new com.jgoodies.looks.windows.WindowsLookAndFeel();
		try {
			UIManager.setLookAndFeel(new WindowsLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {

		}

		// AbstractPersistentCollection.setClientSide(true);
		String[] configLocations = { "classpath:applicationContext.xml" };
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocations, false);
		ProgressSplashScreen progressSplashScreen = new ProgressSplashScreen();
		progressSplashScreen.setShowProgressLabel(true);
		final ProgressBarProgressMonitor tracker = progressSplashScreen.getProgressMonitor();
		context.addBeanFactoryPostProcessor(new ProgressMonitoringBeanFactoryPostProcessor(tracker));
		progressSplashScreen.splash();
		try {
			context.refresh();

			// Application application = GuiLocator.getGui(Application.class);
			MainFrame application = MainFrame.getInstance();

			application.setController(ServiceLocator.getService(ScreenController.class));
			// SecurityContextHolder
			// .setStrategyName(SecurityContextHolder.MODE_GLOBAL);
			application.setTitle(InfoProvider.getApplicationVersion());
			application.getController().start();
		} catch (Exception e) {
			progressSplashScreen.dispose();
			JErrorDialog.showError("Error during startup", e);
			System.exit(-1);
		} finally {
			progressSplashScreen.dispose();
		}
	}

	public static void main(String[] args) throws Exception {
		Starter.getInstance();
	}

	public static Starter getInstance() {
		return instance;
	}

}
